package com.example.webcomen.Controller;

import com.example.webcomen.Common.AjaxResult;
import com.example.webcomen.Model.*;
import com.example.webcomen.Service.BaseInfoService;
import com.example.webcomen.Service.TestService;
import com.example.webcomen.Utill.ToInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class InitController {
    @Autowired
    private TestService testService;
    @Autowired
    private BaseInfoService baseInfoService;
    @GetMapping("test")
    public AjaxResult test(){
        AjaxResult ajaxResult=AjaxResult.success();
        return ajaxResult.put("test",testService.testGroupMenber());
    }
    @GetMapping("/init/me")
    public AjaxResult me(Authentication authentication){
        String name=authentication.getName();
        UserInfo me= ToInfo.toUserInfo(baseInfoService.selectByUserName(name));

        HashMap<String,Object> data=new HashMap<>();
        data.put("mine",me);
        data.put("friend",baseInfoService.findOneFriendGroups(me.getId()));
        data.put("group",baseInfoService.findAllGroupById(me.getId()));
        AjaxResult ajaxResult=AjaxResult.success(data);
        return ajaxResult;
    }
    @GetMapping("init/groups")
    public AjaxResult groups(@RequestParam Integer id){
        Integer gid= id;
        HashMap<String,Object> li=new HashMap<>();
        li.put("list",baseInfoService.findAllUserInTheFriendGroup(gid));
        AjaxResult ajaxResult=AjaxResult.success(li);
        return ajaxResult;
    }
    @GetMapping("init/msgbox/{id}")
    public AjaxResult getAddmessage(@PathVariable("id") String id){
        HashMap<String,Object> li=new HashMap<>();
        List<AddMessage> addMessageInfos=baseInfoService.findOnesAddInvitation(Integer.valueOf(id));
        List<AddMessageFront> frontArrayList=new ArrayList<>();
        if (addMessageInfos!=null){
            addMessageInfos.forEach(addMessageInfo -> {
                frontArrayList.add(ToAddMessageFront(addMessageInfo,ToInfo.toUserInfo(baseInfoService.selecetUserById(addMessageInfo.getFromUid()))));
            });
        }
        li.put("list",frontArrayList);
        AjaxResult ajaxResult=AjaxResult.success(li);
        return ajaxResult;
    }
    @GetMapping("init/msgbox/delete/{mid}")
    public AjaxResult deleteAddmessage(@PathVariable("mid") String mid){
        AjaxResult ajaxResult=null;
        if (baseInfoService.deneyInvit(Integer.valueOf(mid))){
            ajaxResult=AjaxResult.success('成');
        }else {
            ajaxResult=AjaxResult.error();
        }
        return ajaxResult;
    }

    @PostMapping("init/msgbox/{id}/{nowfz}")
    public AjaxResult addmessage(@PathVariable("nowfz") String nowfz,@PathVariable("id") String id,@RequestBody AddMessageFront addMessageFront){
        HashMap<String,Object> li=new HashMap<>();
        AjaxResult ajaxResult=null;
        if (baseInfoService.agreeAddMessageU(nowfz,addMessageFront.getMessageId(),Integer.valueOf(id))) {
            ajaxResult=AjaxResult.success("成功同意申请");
        }else {
            ajaxResult=AjaxResult.error();
        }
        return ajaxResult;
    }
    public AddMessageFront ToAddMessageFront(AddMessage addMessageInfo, UserInfo userInfo){
        AddMessageFront addMessageFront=new AddMessageFront();
        addMessageFront.setMessageId(addMessageInfo.getId());
        addMessageFront.setId(userInfo.getId());
        addMessageFront.setUsername(userInfo.getUsername());
        addMessageFront.setAvatar(userInfo.getAvatar());
        addMessageFront.setType(addMessageInfo.getType()==0? "好友":"群聊申请("+baseInfoService.findGroupNameByGID(addMessageInfo.getGroupId()));
        addMessageFront.setRemark(addMessageInfo.getRemark());
        return addMessageFront;
    }
}
